package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingProjectPlanService;
import com.zzyl.nursing.service.INursingProjectService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author zzyl
 * @date 2025-02-21
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;


    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划VO
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {

        NursingPlan nursingPlan = getById(id);

        List<NursingProjectPlan> list = nursingProjectPlanService.list(new LambdaQueryWrapper<NursingProjectPlan>()
                .eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> nursingProjectPlanVos = BeanUtil.copyToList(list, NursingProjectPlanVo.class);
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan ,vo);
        vo.setProjectPlans(nursingProjectPlanVos);
        return vo;
    }


    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto dto)
    {
        // 属性拷贝
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        save(nursingPlan);


        // 批量保存护理项目计划关系
        dto.getProjectPlans().forEach(v -> v.setPlanId(nursingPlan.getId()));

        nursingProjectPlanService.saveBatch(dto.getProjectPlans());
               return 1 ;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        boolean flag = updateById(nursingPlan);
        if (!flag){
            throw  new BaseException("修改护理计划基本信息失败");
        }
        //删除护理项目
        flag = nursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>()
                .eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
        if (!flag){
            throw  new BaseException("删除护理计划的护理项目信息失败");
        }
        //增加护理项目
        nursingPlanDto.getProjectPlans().forEach(v -> v.setPlanId(nursingPlan.getId()));

        flag = nursingProjectPlanService.saveBatch(nursingPlanDto.getProjectPlans());
        if (!flag){
            throw  new BaseException("增加护理计划的护理项目信息失败");
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long id)
    {
        // 删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        // 删除护理计划
        return removeById(id) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlanVo> selectPlanList() {
      return   nursingPlanMapper.selectPlanList();
    }
}
